{% extends "base.html" %}

{% block headtitle %}Asset Changes on {{ date }}{% endblock %}



{% block header_scripts %}
<script type="text/javascript" src="/m/js/lib/jquery.js"></script>
<script type="text/javascript" src="/m/js/lib/jquery.jstree.js"></script>
{% endblock %}


{% block breadcrumbs %}
<li><a href="/">Home</a></li>
<li><a href="/assets" >Assets</a></li>
<li><a href="/assets/changes" >Changes</a></li>
<li><a href="/assets/changes/{{ date_str }}" >{{ date }}</a></li>
{% endblock %}

{% block main_content %}
<div>
    <h1 class="pagetitle">Asset Changes on <select id="date_selector" class="date_selector">
        {% for d in dates %}
        <option value="{{ d.value }}" {% if d.value == date_str %}selected="selected"{% endif %}>{{ d.show }}</option>
        {% endfor %}
    </select>
</h1>
</div>
<form id="search_form" class="search_form">
    <input type="text" id="search_text" value="" />
    <input type="button" class="button" id="search_button" value="Search" />
    <input type="button" class="button" id="clear_search" value="Reset" />
    <input type="button" class="button" id="expand_all"   value="Expand All" />
    <input type="button" class="button" id="close_all"   value="Close All" />
</form>

<form id="filter_form" class="filter_form" >
    {% for h in hangars %}
    <div>
        <input type="checkbox" id="{{ h.hangarID }}" value="{{ h.hangarID }}" class="hangar-checkbox"
        {% if h.checked %}checked="true"{% endif %} /><span class="checkbox-name">{{ h.name }}</span>
    </div>
    {% endfor %}
    <div><input type="button" class="button" id="filter_divisions" value="Filter Hangars" /></div>
    <div><input type="button" class="button" id="reset_filter_divisions" value="Reset" /></div>
</form>

<div id="assets_tree"></div>

<script type="text/javascript">



$(function () {


    $("#assets_tree").jstree({ 
        "plugins" : [ "themes" , "json_data" , "search" , "adv_search" , "ui",  "types" , "sort" ],
        "core" : { 
            "animation" : 150,
            "html_titles" : true
        },
        "ui" : {
            "select_limit" : 1
        },
        "themes" : {
            "url" : "/m/css/jstree.css",
            "dots" : false
        },
        "search" : {
            "case_insensitive" : true,
            "ajax" : {
                "url" : "/assets/changes/{{ date_str }}/search?since_weeks={{ since_weeks }}&to_weeks={{ to_weeks }}{% if divisions %}&divisions={{ divisions_str }}{% endif %}"
            }
        },
        "types" : {
            "max_depth" : -2,
            "max_children" : -2,
            "valid_children" : [ "station" ],
            "types" : {
                "station" : {
                    "class" : "station-row",
                    "icon" : { 
                        "image" : "/m/img/station.png" 
                    },
                    "valid_children" : [ "hangar" ],
                    "max_depth" : 4,
                    "open_node"   : true,
                    "close_node"  : true
                },
                "hangar" : {
                    "class" : "hangar-row",
                    "icon" : { 
                        "image" : "/m/img/hangar.png" 
                    },
                    "valid_children" : [ "can", "mineral", "ship",  "ammo", "blueprint","item" ],
                    "max_depth" : 3,
                    "open_node"   : true,
                    "close_node"  : true
                },
                "added" : {
                    "icon" : { 
                        "image" : "/m/img/plus-tree-icon.png" 
                    },
                    "valid_children" : "none",
                    "max_depth" : 0,
                    "select_node" : true,
                    "open_node"   : true,
                    "close_node"  : true,
                    "create_node" : false,
                    "delete_node" : false
                },
                "removed" : {
                    "icon" : { 
                        "image" : "/m/img/minus-tree-icon.png" 
                    },
                    "valid_children" : "none",
                    "max_depth" : 0,
                    "select_node" : true,
                    "open_node"   : true,
                    "close_node"  : true,
                    "create_node" : false,
                    "delete_node" : false
                }
            }
        },
        "json_data" : {
            "data" : [
                {% for s in station_list %}
                {
                    "data" : '<b>{{ s.name }}</b><i> - ({{ s.items }} item{{ s.items|pluralize }} changed)</i>',
                    "attr" : { "id" : "_{{ s.locationID }}" , "rel" : "station" , "class" : "station-row" },
                    "state" : "closed"
                },
                {% endfor %}
            ],
            "ajax" : {
                "url" : function(node) { 
                    var url = "/assets/changes/{{ date_str }}";
                    url += node.attr ? node.attr("id").replace(/_/g, "/") : "";
                    url += "?since_weeks={{ since_weeks }}&to_weeks={{ to_weeks }}";
                    {% if divisions %}
                    url += "&divisions={{ divisions_str }}"; 
                    {% endif %}
                    return url; 
                }
            }
        }
    });
});


function getDivisionsStr() {
    var checkboxes = $(".hangar-checkbox");
    var divisions = ""
    for (i=0 ; i < checkboxes.length ; i++) {
        if (checkboxes[i].checked) {
            divisions += "," + checkboxes[i].id
        }
    }
    if (divisions.length != 0) {
        return divisions.substring(1);
    } else {
        return divisions;
    }
}


$("#search_form").submit(function() {
    $("#assets_tree").jstree("search", $("#search_text").val());
    return false;
});

$("#clear_search").click(function() {
    $("#search_text").val("");
    $("#assets_tree").jstree("clear_search");
});

$("#close_all").click(function() {
    $("#assets_tree").jstree("close_all");
});

$("#expand_all").click(function() {
    $("#assets_tree").jstree("open_all");
});



$("#filter_divisions").click(function() {
    var divisions = getDivisionsStr();
    var url = "/assets/changes/{{ date_str }}";
    url += "?since_weeks={{ since_weeks }}&to_weeks={{ to_weeks }}";
    if (divisions.length != 0) {
        url += "&divisions=" + divisions;
    }
    window.location = url;
});

$("#reset_filter_divisions").click(function() {
	window.location = "/assets/changes/{{ date_str }}";
});

$("#date_selector").change(function () {
    var divisions = getDivisionsStr();
    var url = "/assets/changes/" + $("#date_selector option:selected").val();
    url += "?since_weeks={{ since_weeks }}&to_weeks={{ to_weeks }}";
    if (divisions.length != 0) {
        url += "&divisions=" + divisions;
    }
    window.location = url;
});


</script>

{% endblock %}








